//
//  ViewController.m
//  QQMusic
//
//  Created by macbook on 16/7/19.
//  Copyright © 2016年 buWangChuXing. All rights reserved.
//

#import "ViewController.h"
#import "YYModel.h"
#import "XLMusic.h"
#import <Masonry.h>
#import "XLMusicManager.h"

@interface ViewController ()

//存放音乐的数组
@property (nonatomic,strong) NSArray <XLMusic *> *allMusic;
//记录当前歌曲索引
@property (nonatomic, assign) NSInteger currentMusicIndex;
//定时器
@property (nonatomic,strong) NSTimer *updateTimer;


#pragma mark - 竖屏属性
/**
 *专辑配图
 */
@property (weak, nonatomic) IBOutlet UIImageView *albumImageV;
/**
 *  专辑名称
 */
@property (weak, nonatomic) IBOutlet UILabel *albumLabel;
/**
 *  歌词
 */
@property (weak, nonatomic) IBOutlet UILabel *lyricLabel;
/**
 *  歌手
 */
@property (weak, nonatomic) IBOutlet UILabel *singerLabel;
#pragma mark - 横屏属性
/**
 *  横屏专辑配图
 */
@property (weak, nonatomic) IBOutlet UIImageView *HAlbumImage;
/**
 *  横屏歌词
 */
@property (weak, nonatomic) IBOutlet UILabel *HLyricLabel;

#pragma mark - 公共属性
/**
 *  上一曲
 */

@property (weak, nonatomic) IBOutlet UIButton *preButton;
/**
 *  播放
 */
@property (weak, nonatomic) IBOutlet UIButton *playButton;
/**
 *  下一首
 */
@property (weak, nonatomic) IBOutlet UIButton *nextButton;
/**
 *  当前时间
 */
@property (weak, nonatomic) IBOutlet UILabel *currentLabel;
/**
 *  持续时间
 */
@property (weak, nonatomic) IBOutlet UILabel *durationLabel;
/**
 *  进度条
 */
@property (weak, nonatomic) IBOutlet UISlider *progressSlider;
/**
 *  背景视图
 */
@property (weak, nonatomic) IBOutlet UIImageView *bgImageV;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //设置背景图片
    [self setBacnkGrandImage];
    //设置封面视图
    [self settingPareAlbumImageViamge];
    //设置数据
    [self settingData];

}

//设置数据
- (void)settingData
{
    //获取模型
    XLMusic *music = self.allMusic[self.currentMusicIndex];
    //设置背景图
    self.bgImageV.image = [UIImage imageNamed:music.image];
    //设置专辑名称
    self.albumLabel.text = music.album;
    //专辑图片
    self.albumImageV.image = [UIImage imageNamed:music.image];
    //设置歌手
    self.singerLabel.text = music.singer;
    //设置竖向歌词
    //横向歌词
    //横向图片
    self.HAlbumImage.image = [UIImage imageNamed:music.image];
    
    NSLog(@"%@",self.durationLabel.text);
#pragma mark - 一定要先播放在获取时间
    //设置打开软件就唱歌
    [self playBtnClick:self.playButton];
    //设置持续时间 因为获取到的是秒所以得转化下
    self.durationLabel.text = [self timeWithTimeinterVal:[XLMusicManager sharedManager].duration];

    
}

//更新数据
- (void)upCurrentDate
{
    
    //设置当前时间
    self.currentLabel.text = [self timeWithTimeinterVal:[XLMusicManager sharedManager].currtentTime];
    //封面图片旋转
    self.albumImageV.transform = CGAffineTransformRotate(self.albumImageV.transform, M_PI_2 * 0.02);
    //进度条移动
    self.progressSlider.value = [XLMusicManager sharedManager].currtentTime / [XLMusicManager sharedManager].duration;
    //切换下一曲
    if ([self.currentLabel.text isEqualToString:self.durationLabel.text]) {
        [self nextBtnClick:self.nextButton];
    }

    
}



//转化时间间隔为字符串
- (NSString *)timeWithTimeinterVal:(NSTimeInterval)timerInerval
{
    int minute = timerInerval / 60;
    int second =  (int)timerInerval % 60;
    return [NSString stringWithFormat:@"%02d:%02d", minute, second];
}


//设置封面视图
- (void)settingPareAlbumImageViamge
{
    self.albumImageV.layer.cornerRadius = 100;
    self.albumImageV.layer.masksToBounds = YES;
}
//设置背景图片
- (void)setBacnkGrandImage
{
    //设置毛玻璃效果
    //1.设置特效
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    //2.创建视图效果
    UIVisualEffectView *effecV = [[UIVisualEffectView alloc]initWithEffect:blur];
    //3.将显示的视图添加到目标视图中
    [self.bgImageV addSubview:effecV];
    //设置位置
    [effecV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}


//播放
- (IBAction)playBtnClick:(id)sender {
    
    //获取当前模型
    XLMusic *music = self.allMusic[self.currentMusicIndex];
    //判断选中状态
    if (self.playButton.selected) {//暂停
        
        [[XLMusicManager sharedManager] pauseMusic];
        self.playButton.selected = NO;
        //停止定时器
        [self.updateTimer invalidate];
        self.updateTimer = nil;
    }else{ //播放
    
        [[XLMusicManager sharedManager]playMusicWithFileName:music.mp3];
        self.playButton.selected = YES;
        //更新数据
        self.updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(upCurrentDate) userInfo:nil repeats:YES];
        
        [[NSRunLoop currentRunLoop]addTimer:self.updateTimer forMode:NSRunLoopCommonModes];
    }
}
//上一首
- (IBAction)preButtonClick:(id)sender {
    //设置索引
    if (self.currentMusicIndex == 0) {
        
        self.currentMusicIndex = self.allMusic.count - 1;
    }else{
    
        self.currentMusicIndex--;
    }
    
    //设置选中状态
    self.playButton.selected = NO;
    [self.updateTimer invalidate];
    self.updateTimer = nil;
    //设置数据
    [self settingData];
    
}
//下一首
- (IBAction)nextBtnClick:(id)sender {
    if (self.currentMusicIndex == self.allMusic.count - 1) {
        
        self.currentMusicIndex = 0;
    }else{
    
        self.currentMusicIndex++;
    }
    //设置选中状态
    self.playButton.selected = NO;
    //停止定时器
    [self.updateTimer invalidate];
    self.updateTimer = nil;
    //设置数据
    [self settingData];
}

//进度条
- (IBAction)sliderValueChange:(UISlider *)sender
{
    //设置当前时间
    [XLMusicManager sharedManager].currtentTime = sender.value * [XLMusicManager sharedManager].duration;
}


#pragma mark - 懒加载
- (NSArray<XLMusic *> *)allMusic{

    if (_allMusic == nil) {
        
        NSString *path = [[NSBundle mainBundle]pathForResource:@"mlist.plist" ofType:nil];
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
        //进行数据转换
        _allMusic = [NSArray yy_modelArrayWithClass:[XLMusic class] json:dictArray];
    }
    return _allMusic;
}

@end





